<?php

/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


class HTMLHeader
{
	private $refresh;
	private $refreshUrl;
	private $keyword;
	private $description;
	private $title;
	private $css;
	private $favicon;


	public function __construct($title,$css,$keyword,$description,$refresh = false,$refreshUrl="",$favicon = "favicon.png")
	{
		$this->title = $title;
		$this->css = $css;
		$this->keyword = $keyword;
		$this->description = $description;
		$this->refresh = $refresh;
		$this->refreshUrl = $refreshUrl;
		$this->favicon = $favicon;
	}

	public function render()
	{
		$out = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml">
				<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$out .= '<title>'.$this->title.'</title>';
		$out .= '<meta name="keywords" content="'.$this->keyword.'" />';
		$out .= '<meta name="description" content="'.$this->description.'" />';
		$out .= '<link REL="SHORTCUT ICON" HREF="'.$this->favicon.'" />';
		$out .= '<link href="'.$this->css.'" rel="stylesheet" type="text/css" />';

		if($this->refresh)
		{
			$out .= '<meta http-equiv="refresh" content="3;'.$this->refreshUrl.'" />';
		}

		$out .= '</head>';
		return $out;
	}


}


class TemplateFooter {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($footer_text)
	{
		$out = str_replace("<!-- FOOTER TEXT -->",$footer_text,$this->html);
		return $out;
	}
}

class MenuItem {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($text,$link)
	{
		$out = str_replace("<!-- MENUITEM LINK -->",$link,$this->html);
		$out = str_replace("<!-- MENUITEM TEXT -->",$text,$out);
		return $out;
	}
}

class MenuTab {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($text)
	{
		$out = str_replace("<!-- MENUTAB TEXT -->",$text,$this->html);
		return $out;
	}

}

class MenuList {
	private $htmlstart;
	private $htmlend;

	public function __construct($html)
	{
		$tmp = explode("<!-- MENUITEM MARKER -->",$html);
		$this->htmlstart = $tmp[0];
		$this->htmlend = $tmp[1];
	}

	public function renderStart()
	{
		return $this->htmlstart;
	}

	public function renderEnd()
	{
		return $this->htmlend;
	}
}

class MenuHTML {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($menulist)
	{
		$out = str_replace("<!-- MENULIST MARKER -->",$menulist,$this->html);
		return $out;
	}
}

class ContentFrame {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($title,$text)
	{
		$out = str_replace("<!-- FRAME TITLE -->",$title,$this->html);
		$out = str_replace("<!-- FRAME TEXT -->",$text,$out);
		return $out;
	}
}

class ContentWelcome {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($text)
	{
		$out = str_replace("<!-- WELCOME TEXT -->",$text,$this->html);
		return $out;
	}
}

class ContentHTML {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($frame,$welcome = "none")
	{
		$out = $this->html;
		if($welcome != "none")
		{
			$out = str_replace("<!-- WELCOME MARKER -->",$welcome,$out);
		}
		$out = str_replace("<!-- FRAME MARKER -->",$frame,$out);
		return $out;
	}
}

class NavBarLink {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($text,$link)
	{
		$out = str_replace("<!-- NAVLINK -->",$link,$this->html);
		
		$text = str_replace('_','',$text);
		
		$out = str_replace("<!-- NAVTEXT -->",$text,$out);
		return $out;
	}
}

class HeadHTML {
	private $html;

	public function __construct($html)
	{
		$this->html = $html;
	}

	public function render($navlink)
	{
		$out = str_replace("<!-- NAVBARLINK MARKER -->",$navlink,$this->html);
		return $out;
	}
}

class TemplateHead {
	private $headHTML;
	private $navBarLink;

	public function __construct($headHTML,$navBarLink)
	{
		$this->headHTML = $headHTML;
		$this->navBarLink = $navBarLink;
	}

	public function render($navlink_array)
	{
		$out = "";

		foreach($navlink_array as $text => $url)
		{
			$out .= $this->navBarLink->render($text,$url);
		}

		$out = $this->headHTML->render($out);
		return $out;
	}
}

class TemplateContent {
	private $frame;
	private $welcome;
	private $contentHTML;
	private $frameOut;
	private $welcomeText;

	public function __construct($contentHTML,$frame,$welcome)
	{
		$this->contentHTML = $contentHTML;
		$this->frame = $frame;
		$this->welcome = $welcome;
		$this->welcomeText = "none";
		$this->frameOut = "";
	}

	public function addFrame($title,$text){
		$this->frameOut .= $this->frame->render($title,$text);
	}
	
	public function addHTML($html)
	{
		$this->frameOut .= $html;
	}
	
	public function setWelcomeText($welcome)
	{
		$this->welcomeText = $welcome;
	}

	public function render()
	{
		$out = $this->contentHTML->render($this->frameOut,$this->welcomeText);
		return $out;
	}

}

class TemplateMenu {
	private $menuItem;
	private $menuTab;
	private $menuList;
	private $menuHTML;

	public function __construct($menuItem,$menuTab,$menuList,$menuHTML)
	{
		$this->menuItem = $menuItem;
		$this->menuTab = $menuTab;
		$this->menuList = $menuList;
		$this->menuHTML = $menuHTML;
	}

	public function render($menu_array)
	{
		$listStarted = false;
		$menuOut = "";
		foreach($menu_array as $text => $link)
		{
			$text = str_replace('_','',$text);
			$text = str_replace('#','',$text);

			if($link=="MENUTAB")
			{
				if($listStarted)
				{
					$menuOut .= $this->menuList->renderEnd();
					$menuOut .= $this->menuTab->render($text);

					$menuOut .= $this->menuList->renderStart();
				}
				else
				{
					$listStarted = true;
					$menuOut .= $this->menuTab->render($text);

					$menuOut .= $this->menuList->renderStart();
				}
			}
			else
			{
				if($listStarted)
				{
					$menuOut .= $this->menuItem->render($text,$link);
				}
			}
		}

		$menuOut .= $this->menuList->renderEnd();

		$menuOut = $this->menuHTML->render($menuOut);

		return $menuOut;
	}

}

class TemplateError{
	private $html;
	
	public function __construct($html)
	{
		$this->html = $html;
	}
	
	public function render($title,$text)
	{
		$out = str_replace("<!-- ERROR TITLE -->",$title,$this->html);
		$out = str_replace("<!-- ERROR TEXT -->",$text,$out);
		return $out;
	}
}

class Template {
	private $name;
	private $head;
	private $menu;
	private $content;
	private $footer;
	private $error;
	private $out;

	public function __construct($name,$head,$menu,$content,$footer,$error){
		$this->name = $name;
		$this->head = $head;
		$this->menu = $menu;
		$this->content = $content;
		$this->footer = $footer;
		$this->error = $error;
		$this->out = '<body>';
	}
	public function setNavLink($navlink_array)
	{
		$this->out .= $this->head->render($navlink_array);
	}

	public function setMenu($menu_array)
	{
		$this->out .= $this->menu->render($menu_array);
	}
	
	public function addHTML($html)
	{
		$this->content->addHTML($html);
	}

	public function addFrame($title,$text)
	{
		$this->content->addFrame($title,$text);
	}

	public function setWelcomeText($welcome)
	{
		$this->content->setWelcomeText($welcome);
	}
	
	public function renderContent()
	{
		$this->out .= $this->content->render();
	}

	public function setFooterText($text)
	{
		$this->out .= $this->footer->render($text);
	}
	
	public function setError($title,$text)
	{
		$this->out .= $this->error->render($title,$text);
	}

	public function render()
	{
		$this->out .= '</body></html>';
		return $this->out;
	}
}

class HTMLForms {
	
	public function loginForm($lang)
	{
		$html = '<form method="post" action="auth.php">
		<fieldset>
		<legend>'.$lang['login'].'</legend>
		<p><label for="username">'.$lang['username'].' : </label><input type="text" name="username" id="username" tabindex="10" /></p>
		<p><label for="password">'.$lang['password'].' : </label><input type="password" name="password" id="password" tabindex="20" /></p>
		<p><input type="submit" value="'.$lang['get_connected'].'"/></p>
		<p><a href="accountform.php">'.$lang['login_donthaveaccount'].'</a></p>
		</fieldset>
		</form>';
		
		return $html;
	}
	
	public function accountForm($lang)
	{
		$html = '<form method="post" action="newaccount.php">
		<fieldset>
		<legend>'.$lang['newaccount'].'</legend>
		<p><label for="username">'.$lang['username'].' : </label><input type="text" name="username" id="username" tabindex="10" /></p>
		<p><label for="password">'.$lang['password'].' : </label><input type="password" name="password" id="password" tabindex="20" /></p>
		<p><label for="password2">'.$lang['passwordconfirm'].' : </label><input type="password" name="password2" id="password2" tabindex="30" /></p>
		<p><label for="email">'.$lang['email'].' : </label><input type="text" name="email" id="email" tabindex="40" /></p>
		<p><label for="code">'.$lang['verification_code'].' : </label><input type="text" name="code" id="code" tabindex="50" /><br /><img src="newac_img.php" width="120" height="30"></p>
		<p><input type="submit" value="'.$lang['register'].'"/></p>
		</fieldset>
		</form>';
		
		return $html;
	}
}

?>